import 'package:aone_common/common.dart';
import 'package:aone_common/entities/entities.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

class GameCategoryController extends GetxController
    with GetSingleTickerProviderStateMixin {
  static GameCategoryController get to => Get.find();

  GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();

  final RxBool _isGridView = false.obs;
  get isGridView => _isGridView.value;


  final RxBool _isShowProviderSlt = false.obs;
  get isShowProviderSlt => _isShowProviderSlt.value;
  set isShowProviderSlt(value) => _isShowProviderSlt.value = value;

  late final AnimationController animationController;

  final RxBool _isProviderVisible = true.obs;
  get isProviderVisible => _isProviderVisible.value;
  set isProviderVisible(value) => _isProviderVisible.value = value;


  void toggleProviderVisible() {
    animationController.fling(
      velocity: isProviderVisible ? -1.0 : 1.0,
    );
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 350),
      value: 1.0,
    );
    animationController.addStatusListener((status) {
      isProviderVisible = status == AnimationStatus.completed ||
          status == AnimationStatus.forward;
    });
  }

  @override
  void onClose() {
    // TODO: implement onClose
    animationController.dispose();
    super.onClose();
  }

  void onStartGame(dynamic params) {
    // 验证游戏是否为维护状态
    if (params.state == 2) {
      SmartDialog.showToast('game.maintain'.tr);
      return;
    }
    // 公共包无法统一弹出登陆dialog 请自行根据返回的状态处理
    var result = onBeforeGameInit(onGameParams: params);
    if (result == GameInitStatus.invalidUser) {
      // todo show login dialog here
    }
  }

  Animation<RelativeRect> getPanelAnimation(BoxConstraints constraints) {
    final double height = constraints.biggest.height;
    return RelativeRectTween(
      begin: RelativeRect.fromLTRB(0.0, height, 0.0, -height),
      end: const RelativeRect.fromLTRB(0.0, 0.0, 0.0, 0.0),
    ).animate(
      CurvedAnimation(parent: animationController, curve: Curves.linear),
    );
  }

  void toggleViewType() => _isGridView.value = !_isGridView.value;
}
